const {createClient} = require("redis")
const config = require("../config")
class Redis {
    client = null
    constructor() {
        this.client = createClient(config.redis)
        this.client.on("error", err => {
            console.log('Redis Client Error', err)
        })
        this.#link()
    }
    #link () {
        return new Promise((resolve, reject) => {
            this.client.connect().then(value => {
                resolve(value)
            }, err => {
                reject(err)
            })
        })
    }
    setItem (key, value, sec) {
        this.client.set(key, value, {
            EX: sec
        })
    }
    getItem (key) {
        return new Promise((resolve, reject) => {
            this.client.get(key).then(value => {
                resolve(value)
            }).catch((err) => {
                reject(err)
            })
        })
    }
    clear (key) {
        this.#link().then(() => {
            this.client.del(key)
        })
    }

    quit () {
        this.client.quit()
    }
}

module.exports = Redis